<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="storeName">
        <el-input v-model="queryParams.storeName" :placeholder="storeInfo.storeName" disabled clearable />
      </el-form-item>

      <el-form-item label="提交时间" prop="submitTime">
        <el-date-picker clearable
                        v-model="queryParams.submitTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择提交时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
              v-for="dict in restock_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">

        <template #default="scope">
          <router-link :to="{ name: 'PurchaseOrder', query: { storeId: storeInfo.id } }"
                       class="link-type"
                       v-hasPermi="['restock:restock:add']">
            <el-button
                type="primary"
                plain
                icon="Plus">新增
            </el-button>
          </router-link>
        </template>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['restock:restock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['restock:restock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="restockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="清单编号" align="center" prop="replenishId" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
<!--        <template #default="scope">-->
<!--          <dict-tag :options="restock_type" :value="scope.row.status"/>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">

          <router-link :to="{name:'PurchaseOrder', query:{restockId:  scope.row.replenishId} }" class="link-type">
            <el-button link type="primary" icon="Edit" v-if="scope.row.status === '已保存，未提交'" v-hasPermi="['restock:restock:edit']">修改</el-button>
          </router-link>

          <router-link :to="{name:'PurchaseOrder', query:{restockId:  scope.row.replenishId} }" class="link-type">
            <el-button link type="primary" v-hasPermi="['restock:restock:list']">详情</el-button>
          </router-link>
          <el-button link type="primary"  @click="handleDelete(scope.row)" v-hasPermi="['restock:restock:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改存储补货请求的对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="restockRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺ID" />
        </el-form-item>
        <el-form-item label="提交时间" prop="submitTime">
          <el-date-picker clearable
                          v-model="form.submitTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in restock_type"
                :key="dict.value"
                :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listRestock, getRestock, delRestock, addRestock, updateRestock,queryStoreInfo } from "@/api/restock/restock";

const { proxy } = getCurrentInstance();
const { restock_type } = proxy.useDict('restock_type');

const restockList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const storeInfo = ref([]);
const router = useRouter();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    storeId: null,
    submitTime: null,
    status: null,
    description: null,
    storeName: null
  },
  rules: {
    storeId: [
      { required: true, message: "店铺ID不能为空", trigger: "blur" }
    ],
    submitTime: [
      { required: true, message: "提交时间不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询门店名称 **/
function getStoreInfo() {
  queryStoreInfo().then(response => {
    storeInfo.value = response.data;
    queryParams.value.storeName = storeInfo.value.storeName;

  });
  getList();
}

/** 查询存储补货请求的列表 */
function getList() {
  loading.value = true;
  listRestock(queryParams.value).then(response => {
    restockList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    replenishId: null,
    storeId: null,
    submitTime: null,
    status: null,
    description: null
  };
  proxy.resetForm("restockRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.replenishId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _replenishId = row.replenishId || ids.value
  getRestock(_replenishId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改存储补货请求的";
  });
}

/** 详情按钮操作 */
function handleRestockProduct(row) {
  const _replenishId = row.replenishId || ids.value
  proxy.$router.push({ path: '/restock/restockProduct', query: { replenishId: _replenishId }});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["restockRef"].validate(valid => {
    if (valid) {
      if (form.value.replenishId != null) {
        updateRestock(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRestock(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _replenishIds = row.replenishId || ids.value;
  proxy.$modal.confirm('是否确认删除存储补货请求的编号为"' + _replenishIds + '"的数据项？').then(function() {
    return delRestock(_replenishIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('restock/restock/export', {
    ...queryParams.value
  }, `restock_${new Date().getTime()}.xlsx`)
}

getStoreInfo();

</script>